package myPackage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserManager extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UserManager() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					
		ArrayList<Utilisateur> utilisateur=null;
		Utilisateur tmpUser = new Utilisateur();
				
		if(request.getParameter("plusnom")!=null && request.getParameter("plusnom")!=""){
			tmpUser.setNom(request.getParameter("plusnom"));
			tmpUser.setPassword(request.getParameter("pluspassword"));	
			if(request.getParameter("plustype")!=null ){
				tmpUser.setUserType(true);
			}
		}
		
		utilisateur = (ArrayList<Utilisateur>) request.getServletContext().getAttribute("users");
		if(utilisateur!=null) {
			utilisateur.add(tmpUser);
			System.out.println(utilisateur.size());
		}
		
		
		
		PrintWriter out = response.getWriter();
		
		out.println("<html>");
		out.println("<head>");
			out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
			out.println("<title>UserManagerlvkjqerlkgj</title>");
		out.println("</head>");
		
		ArrayList<String> nomUtili= new ArrayList<String>(); //affichage
		String tmp;		
		if(utilisateur!=null) {								//affichage
			for(int i=0; i<utilisateur.size(); i++) {
				tmp = (String) utilisateur.get(i).getNom();
				nomUtili.add(tmp);
			}
		}
		
		out.println("<body>");	
		out.println("<h3></h3>");
		if(utilisateur!=null){								//affichage
			for (String string : nomUtili) {
				out.println("<h1>");	
				out.println(string);
				out.println("<h1>");
			}
		}
		
		out.print("</body></html>");	
	}

}
